from util import app, serializer, db
from muffin import ResponseJSON

@app.route('/')
@serializer(ResponseJSON)
async def hello(request):
    for item in range(10000):
        pass
    return {
        'msg': 'ok'
    }

@app.route('/model', methods=['GET'])
@serializer(ResponseJSON)
async def model(request):
    rows = await db.fetch_all('SELECT * from moji limit 0,5')

    # res = await db.fetch_one('SELECT * from moji limit 0,1')

    # fetch_val execute iterate transaction

    # data = await request.data()
    # await db.execute_many('INSERT INTO items (name, value) VALUES (:name, :value)', values=data)
    return [dict(row) for row in rows]

@app.route('/session')
async def hello(request):
    # seesion
    request.session['var'] = 'value'
    # request.session.clear()
    rs = request.session.get('var')
    return {
        'msg': rs
    }

@app.route('/hi/{name}')
async def hello(request):
    name = request.path_params.get('name', 'world')
    return {
        'msg': name.title()
    }
